/* 
 * Author: Alexander Rauser, arauser.org
 * Date: August 2008
 * Copyright: 2008
 * Licensed under Creative Commons Attribution 3.0 Unported
 */
 
package org.arauser.uploader.view
{
	
	import flash.desktop.*;
	import flash.events.*;
	import flash.filesystem.File;
	
	import mx.managers.PopUpManager;
	
	import org.arauser.uploader.ApplicationFacade;
	import org.arauser.uploader.view.components.Configuration;
	import org.arauser.uploader.view.components.UploadItem;
	import org.puremvc.as3.interfaces.*;
	import org.puremvc.as3.patterns.facade.*;
	import org.puremvc.as3.patterns.mediator.*;
	
	public class ApplicationMediator extends Mediator implements IMediator
	{
		
		
		
		// Cannonical name of the Mediator
        public static const NAME:String = 'ApplicationMediator';
 
        // Constructor 
        public function ApplicationMediator(viewComponent:Uploader) {
            
            super( NAME, viewComponent );
            
            // Listen for events from the view component
            viewComponent.addEventListener(NativeDragEvent.NATIVE_DRAG_ENTER, onDragIn);
            viewComponent.addEventListener(NativeDragEvent.NATIVE_DRAG_DROP, onDragDrop );
            viewComponent.background.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
            viewComponent.addEventListener("CLOSE", onClose);
            viewComponent.addEventListener("MINIMIZE", onMinimize);
            viewComponent.addEventListener("CONFIGURATION", onConfiguration);
        }
 
 		/**
 		 * Moves the application window when holding the background image.
 		 */
 		private function onMouseDown(event:MouseEvent):void
 		{
 			viewComponent.stage.nativeWindow.startMove();
 		}
 		
 		/**
 		 * Minimize the application window
 		 */
 		private function onMinimize(evt:MouseEvent):void
		{
		    viewComponent.stage.nativeWindow.minimize();
		}
		
		/**
		 * Creates a popup from the Configuration component and registers the Mediator.
		 */
		private function onConfiguration(event:MouseEvent):void
		{
			var config:Configuration = new Configuration();
			PopUpManager.addPopUp( config, uploader, true);
			PopUpManager.centerPopUp(config);
			facade.registerMediator( new ConfigurationMediator( config ) );         
   		}
		
		/**
		 * Close the application
		 */
		private function onClose(evt:MouseEvent):void
		{
		    viewComponent.stage.nativeWindow.close();
		}
		
 		/**
 		 * Accepts the drag operation from the OS if the clipboard contains a list of file names.
 		 */
        private function onDragIn(e:NativeDragEvent):void {
        	
	     	//check and see if files are being drug in
			if(e.clipboard.hasFormat(ClipboardFormats.FILE_LIST_FORMAT))
			{
				// Accept the drag operation
				NativeDragManager.acceptDragDrop(uploader);
			}
		}
        
        /**
        * Gets the Array of files from the clipboard and uploads each file seperately.
        */
        private function onDragDrop(e:NativeDragEvent):void 
        {
            var arr:Array = e.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;
		
			for each (var f:File in arr)
		    {
		    	uploadFile( File(f) );
		    }		    
        }
        
		/**
		 * Return list of Nofitication names that Mediator is interested in
		 */
        override public function listNotificationInterests():Array {
            return [ ApplicationFacade.SETCONFIG ];
        }
 		
 		/**
 		 * Handle all notifications this Mediator is interested in
 		 */
        override public function handleNotification(note:INotification):void  {
            switch (note.getName()) {
                case ApplicationFacade.SETCONFIG:
                	// This triggers if the upload URL wasn't set yet
                	onConfiguration(new MouseEvent(MouseEvent.CLICK));
                    break;
            }
        }
        
        /**
        * Creates a new UploadItem, add it to the list. 
        * The file is passed to the mediator as well as a unique id,
        * we need a unique id in order to create multiple mediators for each upload item
        * and be able to remove each individually or listen to notifications.
        */
        private function uploadFile(file:File):void
		{
			var uploadItem:UploadItem = new UploadItem();
			uploader.itemList.addChild(uploadItem);
			
			facade.registerMediator(new UploadItemMediator(uploadItem, file, file.name));		
		}
    	
 		/**
 		 * Cast the viewComponent to its actual type.
 		 */
		protected function get uploader() : Uploader {
			return viewComponent as Uploader;
		}
	}
}